<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>

<div class="table_div">
  <div id="searchParam">
    <div class="layui-form-item">
      <div class="layui-input-inline layui-form">
      	<select id="taxorgcodeInput">
		</select>
	  </div>
      <div class="layui-input-inline">
     	<input type="text" id="stationNameInput" class="layui-input"  autocomplete="off" placeholder="请输入加油站名称">
   	  </div>
	  <div class="layui-input-inline">
        <input type="text" id="createTimeInput" class="layui-input"  autocomplete="off" placeholder="请输入时间范围">
      </div>
			      
      <div class="layui-input-inline ">
        <button class="layui-btn" onclick="search()"  id="search">查询</button>
        <button class="layui-btn"   id="export">导出全部</button>
      </div>
    </div>

  </div>
  <table class="layui-table" id="showTable" lay-filter="showTable" ></table>
</div>
<script type="text/html" id="toolbar">
  
</script>
<script type="text/html" id="tool">
  
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
  //获取token
  var token = CoreUtil.getData("access_token");
  //地址栏转义token中的#号
  var tokenQuery = token.replace("#", "%23");
  var tableIns1;
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var $ = jQuery = layui.jquery;
  var laydate = layui.laydate;
  var firstDayOfMonth = firstDayOfMonth();
  var lastDayOfMonth = lastDayOfMonth();
  var startTime1 = "";
  var endTime1 = "";
  var startTime2 = "";
  var endTime2 = "";
  
  console.log(firstDayOfMonth);
  console.log(lastDayOfMonth);
  console.log(firstDayOfMonth + " 至 " + lastDayOfMonth);
  
  $("#createTimeInput").val(firstDayOfMonth + " 至 " + lastDayOfMonth);

  $(function() {
	  // 加载机关
      CoreUtil.sendGet("/sys/deptlist",null , function(res) {
			if (res.code == '0') {
				var value = res.data[0];
				var opt = "<option value = ''>请选择所属机关</option>";
				for (var i in value) {
					opt += '<option value = "' + value[i].id + '">'
							+ value[i].name + '</option>';
				}
				
				$("#taxorgcodeInput").html(opt);
				form.render('select'); //需要渲染一下
			} else {
				layer.msg(res.msg);
				return false;
			}
		}, false, false);
		
  });
  
  
  layui.use(['table', 'layer', 'laydate'], function () {
	
		laydate.render({
			elem : '#createTimeInput',
			type : 'date',
			range : '至',
			done : function(value, date, endDate) {
				if (value != null && value != undefined
						&& value != "") {
					startTime1 = value.split("至")[0];
					endTime1 = value.split("至")[1];
					
				} else {
					startTime1 = null;
					endTime1 = null;
				}

			}
		});  
	  
	  
    //加载table
    tableIns1 = table.render({
      elem: '#showTable'
      , contentType: 'application/json'
      , headers: {"authorization": token}
      , page: true //开启分页
      , url: '/gas/detection/list' //数据接口
      , method: 'POST'
      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
        return {
          "code": res.code, //解析接口状态
          "msg": res.msg, //解析提示文本
          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
          "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
        }
      }
      , cols: [
        [
          {type: 'checkbox', fixed: 'left'},
          {field: 'id', title: '主键', sort: true, hide: true},
          
          {field: 'stationId', title: '加油站ID', sort: true, hide: true},
          {field: 'stationinfoName', title: '加油站名称', sort: true, },
          
          {field: 'camId', title: '摄像机编号', sort: true},
          {field: 'taxorgcode', title: '税务机关ID', sort: true, hide: true},
          {field: 'taxorgName', title: '税务机关', sort: true},
          
          {field: 'result', title: '调用结果', sort: true},
          
          {field: 'picTime', title: '图片时间', sort: true},
          {field: 'createTime', title: '创建时间', sort: true},
          {field: 'pic', title: '图片地址', sort: true, templet: function(d) {
				if(!CoreUtil.isEmpty(d.pic)){
					return '<a href="javascript:;" data-src="'+d.pic+'" class="layui-table-link" onclick="showImage(this)">查看图片</a>'
				}
		  }}
        ]
      ]
      , toolbar: '#toolbar'
    });


    //表头工具
    table.on('toolbar(showTable)', function(obj){
      switch(obj.event){
      };
    });
    //列操作
    table.on('tool(showTable)',function (obj) {
    });

    //导出
    $('#export').on('click', function () {
    	var createTimeInput = $("#createTimeInput2").val();
   	  	var arr = createTimeInput.split('至');
    	
    	//原先分页limit
      	var exportParams = {
    		  limit: -1,
  	          taxorgcode: $("#taxorgcodeInput").val(),
  	  		  stationinfoName: $("#stationNameInput").val(),
        	  createTimeStart: arr[0],
  	  		  createTimeEnd: arr[1],
      	};
      CoreUtil.sendPost("/gas/detection/list", exportParams, function (res) {
        //初始化渲染数据
        if (res.data != null && res.data.records != null) {
          table.exportFile(tableIns1.config.id, res.data.records, 'xls');
        }
      });
    });

    //返回
    $("#btn_cancel").click(function() {
      $(".table_div").show();
      $(".operation").hide();
      return false;
    });

  });

  //执行查询
  function search() {
	  var createTimeInput = $("#createTimeInput").val();
 	  var arr = createTimeInput.split('至');
 	  
	  //这里以搜索为例
      tableIns1.reload({
      	where: { //设定异步数据接口的额外参数，任意设
	  		taxorgcode: $("#taxorgcodeInput").val(),
	  		stationinfoName: $("#stationNameInput").val(),
	  		createTimeStart: arr[0],
	  		createTimeEnd: arr[1],
      	}
      	, page: {
        	curr: 1 //重新从第 1 页开始
      	}
    });
  };
  
  
	function firstDayOfMonth() {
  		const currentDate = new Date();
  		const year = currentDate.getFullYear();//获取当前年
  		const month = String(currentDate.getMonth() + 1).padStart(2, "0");//获取当前月
  		const firstDay = "01";//日
  		return `${year}-${month}-${firstDay}`;
    };
	
    function lastDayOfMonth() {
    	// 获取当前日期
    	const currentDate = new Date();
    	// 获取当前年份
    	let year = currentDate.getFullYear();
    	// 获取下个月的月份，使用 padStart 在月份小于 10 时在前面补零
    	let month = String(currentDate.getMonth() + 2).padStart(2, "0");
    	// 如果月份为 '13'，说明当前是12月，需要更新年份并将月份设置为 '01'
    	if (month === '13') {
    	    year = year + 1;
    	    month = '01';
    	}
    	 
   	  	// 设置每个月的第一天为 '01'
   	  	let firstDay = "01";
   	  	// 构建下个月的第一天的日期字符串，格式为 'YYYY-MM-DD'
   	  	const firstDayOfNextMonth = `${year}-${month}-${firstDay}`;
   	  	// 计算当前月的最后一天，通过减去一天的毫秒数来得到
   	  	const lastDayOfMonth = new Date(new Date(firstDayOfNextMonth).getTime() - 86400000);
   	  	// 将最后一天的日期转换为 ISO 格式，并提取日期部分
   	  	return lastDayOfMonth.toISOString().split("T")[0];
	}
    
	function showImage(obj) {
		var src = $(obj).attr('data-src'); // 获取图片地址
	  layer.open({
	    type: 1,
	    title: false,
	    closeBtn: 0,
	    area: '500px',
	    skin: 'layui-layer-nobg', // 没有背景色的弹层
	    shadeClose: true,
	    content: '<img src="' + src + '" alt="图片" style="margin: auto; max-width: 100%; max-height: 100%">'
	  });
	};
</script>